package com.tyj.tuyijieapp.controller;


import com.tyj.tuyijiesystem.model.juser.entity.JUser;
import com.tyj.tuyijiesystem.model.juser.service.JUserService;
import com.tyj.tuyijiecommon.exception.UpdateException;
import com.tyj.tuyijiecommon.util.Result;
import com.tyj.tuyijiecommon.util.SmsUtil;
import io.swagger.annotations.*;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

/**
 * <p></p>
 *
 * @author lhf
 * @since 2020/6/8
 */
@RestController
@RequestMapping("j-user")
@Api(tags = "用户接口")
public class JUserController {

    @Resource
    private JUserService jUserService;
    @Resource
    private SmsUtil smsUtil;

    @PostMapping("register")
    @ApiOperation("注册")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "phone", value = "手机号", required = true, dataTypeClass = String.class, example = "13091326585"),
            @ApiImplicitParam(name = "code", value = "验证码", required = true, dataTypeClass = String.class, example = "254869")
    })
    @ApiResponses({
            @ApiResponse(code = 2000, message = "请求成功"),
            @ApiResponse(code = 5000, message = "请求失败"),
    })
    public Result<Boolean> register(String phone, String code, String password) {
        return Result.success(this.jUserService.insertSelective(new JUser(phone), code, password));
    }

    @GetMapping("code")
    @ApiOperation(value = "获取验证码")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "phone", value = "手机号", required = true, dataTypeClass = String.class, example = "13091326585"),
            @ApiImplicitParam(name = "flag", value = "标识 0 注册 1 登录", required = true, dataTypeClass = Integer.class, example = "123456")
    })
    @ApiResponses({
            @ApiResponse(code = 2000, message = "请求成功"),
            @ApiResponse(code = 5000, message = "请求失败"),
    })
    public Result<String> getCode(String phone, int flag) throws Exception {
        String code = null;
        switch (flag) {
            case 0: {
                code = smsUtil.register(phone);
                break;
            }
            case 1: {
                code = smsUtil.login(phone);
                break;
            }
        }

        return Result.success(code);
    }


    @GetMapping("exist")
    @ApiOperation("查询用户是否存在")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "phone", value = "手机号", required = true, dataTypeClass = String.class, example = "13091326582")
    })
    @ApiResponses({
            @ApiResponse(code = 2000, message = "请求成功"),
            @ApiResponse(code = 5000, message = "请求失败"),
    })
    public Result<Boolean> existJUser(String phone) {
        return Result.success(this.jUserService.existJUser(phone));
    }

    @PutMapping("update")
    @ApiOperation("更新用户信息，完善用户信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "主键，要更新的用户", required = true, dataTypeClass = String.class, example = "1"),
            @ApiImplicitParam(name = "nickName", value = "昵称", required = false, dataTypeClass = String.class, example = "葬月"),
            @ApiImplicitParam(name = "realName", value = "真实名字", required = false, dataTypeClass = String.class, example = "张铎"),
            @ApiImplicitParam(name = "headImg", value = "头像", required = false, dataTypeClass = String.class, example = "http://www.baidu.com"),
            @ApiImplicitParam(name = "password", value = "密码", required = false, dataTypeClass = String.class, example = "123465"),
            @ApiImplicitParam(name = "signature", value = "个性签名", required = false, dataTypeClass = String.class, example = "路很长，但是并不妨碍我走到终点"),
            @ApiImplicitParam(name = "empiricalValue", value = "经验值", required = false, dataTypeClass = String.class, example = "0"),

    })
    @ApiResponses({
            @ApiResponse(code = 2000, message = "请求成功"),
            @ApiResponse(code = 5000, message = "请求失败"),
    })
    public Result<JUser> updateByPrimaryKeySelective(JUser record) throws UpdateException {
        return Result.success(this.jUserService.updateByPrimaryKeySelective(record));
    }

    @PutMapping("pass")
    @ApiOperation("修改密码 0 修改失败 1 修改成功 -1 密码不正确")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "newVal", value = "新密码", required = true, dataTypeClass = String.class, example = "123456"),
            @ApiImplicitParam(name = "oldVal", value = "旧密码", required = true, dataTypeClass = String.class, example = "123456"),
    })
    @ApiResponses({
            @ApiResponse(code = 2000, message = "请求成功"),
            @ApiResponse(code = 5000, message = "请求失败"),
    })
    public Result<Integer> updatePassword(String newVal, String oldVal, Long uid, HttpServletRequest request) {
        return Result.success(this.jUserService.updatePassword(newVal, oldVal, uid));
    }
}
